package com.example.expinterview.exception;

import com.example.expinterview.entity.ResultBody;
import com.example.expinterview.entity.enums.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @classname: GlobalExceptionHandler
 * @description: TODO 全局异常处理
 * @author: kaiser
 * @date: 2021/8/31 13:32
 * @version: 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(InterviewException.class)
    public ResultBody interviewException(InterviewException exception){
        log.info("业务异常,原因是：{}", exception.getErrorMsg());
        return ResultBody.fail(exception.getErrorCode(), exception.getErrorMsg());
    }

    @ExceptionHandler(NullPointerException.class)
    public ResultBody nullException(NullPointerException exception) {
        log.info("空指针异常", exception);
        return ResultBody.fail(ErrorCodeEnum.NULL_EXCEPTION);
    }

    @ExceptionHandler(Exception.class)
    public ResultBody systemException(Exception exception) {
        log.info("系统执行出现异常", exception);
        return ResultBody.fail(ErrorCodeEnum.SYSTEM_ERROR);
    }
}
